package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

	@Reference
	OrderSettingService orderSettingService;

	@RequestMapping("/upload")
	public Result upload(MultipartFile excelFile){
		//把从excel中的读取到的数据添加到集合中
		List<OrderSetting> orderSettingList = new ArrayList<>();
		try {
			//获取流对象
			InputStream inputStream = excelFile.getInputStream();
			//获取工作薄对象
			XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
			//获取工作表对象
			XSSFSheet sheet = workbook.getSheetAt(0);
			//获取excel中的数据
			//最后一行的行号
			int lastRowNum = sheet.getLastRowNum();
			// 从第二行开始（第一行是表头）
			for (int i = 1; i <= lastRowNum; i++) {
				OrderSetting orderSetting  = new OrderSetting();

				//获取每一行对象
				XSSFRow row = sheet.getRow(i);
				//获取每一列
				XSSFCell cell0 = row.getCell(0);
				Date dateCellValue = cell0.getDateCellValue();
				orderSetting.setOrderDate(dateCellValue);
				//System.out.println("dateCellValue"+dateCellValue);

				XSSFCell cell1 = row.getCell(1);
				Double numericCellValue = cell1.getNumericCellValue();
				//System.out.println(numericCellValue);

				orderSetting.setNumber(numericCellValue.intValue());

				//把预约设置对添加到集合
				orderSettingList.add(orderSetting);
			}
			log.debug("orderSettingController:upload"+orderSettingList);

			orderSettingService.addOrderSettingList(orderSettingList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
		}
		return new Result(true,MessageConst.IMPORT_ORDERSETTING_SUCCESS);
	}

	@RequestMapping("/getOrderSettingByMonth")
	public Result getOrderSettingByMonth(String date ) throws Exception {

		List<OrderSetting> orderSettingList = orderSettingService.getOrderSettingByMonth(date);

		List<Map<String,Object>> mapList = new ArrayList<>();

		for (OrderSetting orderSetting : orderSettingList) {

			Map<String,Object> map = new HashMap<>();
			map.put("date", new SimpleDateFormat("dd").format(orderSetting.getOrderDate()));
			map.put("number",orderSetting.getNumber());
			map.put("reservations",orderSetting.getReservations());
			mapList.add(map);
		}
		log.debug("获取预约设置信息成功");
		log.debug(mapList.toString());
		return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS, mapList);



	}
	@RequestMapping("/saveOrEdit")
	public Result saveOrEdit(@RequestBody OrderSetting orderSetting){
		log.debug("OrderSettingController:saveorEdit:" + orderSetting);
		orderSettingService.add(orderSetting);
		log.debug("更新预约设置信息成功！！");
		return new Result(true,MessageConst.ORDERSETTING_SUCCESS);

	}



}
